package cn.stronglink.asset.module.dam.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.stronglink.asset.model.CabinetExt;
import cn.stronglink.asset.model.SensorSetting;
import cn.stronglink.asset.module.assetmanage.cabinet.mapper.CabinetExtMapper;
import cn.stronglink.asset.module.dam.entity.dev.RackInfo;
import cn.stronglink.asset.module.dam.entity.message.MsgRealTimeDatas;
import cn.stronglink.asset.module.dam.interfaces.RealtimeDatasHandleStrategyI;
import cn.stronglink.asset.module.dam.mapper.DamSensorMapper;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive.IntercepterRealTimeDatasUtil;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive.ListContainerUtil;

@Service("toDoorDbService")
public class RealTimeSendToDoorDb implements RealtimeDatasHandleStrategyI {
	// 共6个传感器实时数据 + 2个list存放sensro温湿度 + 2个list存放cabinet门数据
		private ListContainerUtil listContainer = new ListContainerUtil();

	@Autowired
	private CabinetExtMapper cabinetExtMapper;
	@Autowired
	private DamSensorMapper damSensorMapper;
	
	// 持有数据: 容器--存放最新的门数据
	private CabinetExt cabinetExt = new CabinetExt();

	@Override
	public synchronized void handleDamDatas(MsgRealTimeDatas realDatas) {
		listContainer.clearAllDoorSensor();//清空容器

		if (IntercepterRealTimeDatasUtil.doorDiff<5000) {
			return;// 未到5s
		}

		
		SensorSetting[] senList =realDatas.getAwsPostdata().getSensorList();
		String rackConverCode = realDatas.getAwsPostdata().getRackConverCode();
		RackInfo rackinfo = damSensorMapper.getRackInfo(rackConverCode);
		boolean cabUp = false;
		boolean cabIn = false;
		
		
		// 查询传感器信息： 是否已存在
		if (rackinfo.getRackId() != 0) {// 存在该机柜,更新数据

			for (SensorSetting sen : senList) {
				if (sen.getSensorType() == 3) {// 6个传感器server：调用此服务
					
					// 默认: 只有一个门传感器: 数据对象只保存最后的数据
					cabinetExt.setAssetId(rackinfo.getRackId());
					cabinetExt.setOnlineTime(new Date());// long 转date
					cabinetExt.setDoorStatus(sen.getSensorDatas());
					System.out.println("门id--------" + rackinfo.getCabinetId());

					if (rackinfo.getCabinetId() > 0) {// 存在,更新
						cabUp = true;
						listContainer.getCabUplist().add(cabinetExt);
					} else {// 不存在,插入
						cabIn = true;
						cabinetExt.setCreateTime(new Date());
						listContainer.getCabInlist().add(cabinetExt);
					}
				} // type=3
			} // for结束
			
			// 批量更新或增加 
			if (cabIn) {// cab表： 插入
				cabinetExtMapper.insertCabinetExtByDam(listContainer.getCabInlist());
				System.out.println("插入cab结束。。。。。。\n");
			}
			if (cabUp) {
				cabinetExtMapper.updateCabinetExtHeatBeat(listContainer.getCabUplist());
				System.out.println("更新cab结束。。。。。。\n");
			}

		} // 存在该机柜
		else {
			System.out.println("查询..无此机柜, 无效的rackConverCode");
		}
	}

	@Override
	public boolean isHandle(MsgRealTimeDatas realDatas) {
		SensorSetting[] senArr = realDatas.getAwsPostdata().getSensorList();

		for (SensorSetting sen : senArr) {
			if (sen.getSensorType() == 3) {// 判断温湿度表
				return true;
			}
		}
		return false;
	}
}
